package com.foo.example.action;

import java.io.IOException;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foo.common.base.action.FooGenericAction;
import com.foo.common.base.utils.FooUtils;
import com.foo.example.model.MyExample;
import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.gson.Gson;

@Results({@Result(name = "404", location = "/WEB-INF/public/404.jsp")})
@ParentPackage("example")
public class ExampleAction extends FooGenericAction {

    private static final long serialVersionUID = 3609540005224931735L;
    Logger logger = LoggerFactory.getLogger(ExampleAction.class);

    /**
     * 返回json字符串
     */
    @Action("/example/testForCrud")
    public void testForCrud() {

        logger.info("testForCrud");

        MyExample myExample = new MyExample();
        myExample.setExpStr("中文字段测试");
        logger.info("saving...");
        fooGenericService.save(myExample);

        myExample.setExpInt(100);
        logger.info("updating...");
        fooGenericService.update(myExample);

        logger.info("deleting...");
        fooGenericService.delete(myExample);

        FooUtils.printSuccessJson(response, myExample);
    }

    /**
     * 事务测试
     */
    @Action("/example/testForTransaction")
    public void testForTransaction() {

        logger.info("testForTransaction");

        MyExample myExample = new MyExample();
        myExample.setExpStr("中文字段测试");

        fooGenericService.runInTransaction(() -> {
            logger.info("saving...");
            fooGenericService.save(myExample);
            if (1 == 1) {
                throw new RuntimeException("if you see this error,it means internal test only...");
            }
        });
        FooUtils.printSuccessJson(response, myExample);
    }

    /**
     * 返回json字符串
     */
    @Action("/example/testForJson")
    public void testForJson() {
        FooUtils.printSuccessJson(response);
    }

    /**
     * 返回jsp页面
     */
    @Action(value = "/example/testForDispatcher", results = {
            @Result(name = "success", location = "/WEB-INF/pages/example/testForDispatcher.jsp", type = "dispatcher")})
    public String testForDispatcher() {
        logger.info("testForDispatcher");
        return "success";
    }

    /**
     * 调用特殊拦截器的方法
     */
    @Action(value = "/example/testForInterceptor")
    public void testForInterceptor() {
        logger.info("testForInterceptor");
        FooUtils.printSuccessJson(response);
    }

    /**
     * 测试异常
     */
    @Action(value = "/example/testForException")
    public void testForException() {
        logger.info("testForException");
        throw new RuntimeException("testForException");
    }

    @Action(value = "/example/helloWorld")
    public void helloWorld() throws IOException {
        Map<String, Object> myResultMap = Maps.newHashMap();
        myResultMap.put("ret", 0);
        myResultMap.put("msg", "");
        Gson gson = new Gson();
        response.setCharacterEncoding(Charsets.UTF_8.toString());
        response.setContentType("text/plain");
        response.getWriter().write(gson.toJson(myResultMap));
    }

}
